import 'package:flutter/material.dart';
import 'package:flutter_1911/model/common_model.dart';
import 'package:flutter_1911/until/nav_until.dart';
import 'package:flutter_1911/widget/webview.dart';

//活动入口 跟球区入口很像 只不过一个是一行 一个是两行
class Sub extends StatelessWidget {
  final List<CommonModel> subList;

  const Sub({Key? key, required this.subList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        //设置四个角都是圆角
        borderRadius: BorderRadius.circular(6),
      ),
      child: Padding(
        padding: const EdgeInsets.all(7),
        child: _subitems(context),
      ),
    );
  }

  _subitems(BuildContext context) {
    if (subList == null) {
      return null;
    }
    List<Widget> item = [];
    subList.forEach((element) {
      item.add(_subitem(context, element));
    });
    //计算出第一行显示的数据
    int separte = (subList.length / 2 + 0.5).toInt();
    //因为是上下两行 所以用Column接收一下
    return Column(
      children: [
        //这是第一行
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //裁接一下
          children: item.sublist(0, separte),
        ),
        //因为距离第一行有边距所以用Padding
        Padding(
          padding: const EdgeInsets.only(top: 10),
          //第二行数据
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: item.sublist(separte, subList.length),
          ),
        )
      ],
    );
  }

  Widget _subitem(BuildContext context, CommonModel element) {
    //平分布局大小
    return Expanded(
        flex: 1,
        child: GestureDetector(
          onTap: () {
            NavigatorUtil.push(
                context,
                HiWebView(
                  url: element.url,
                  statusBarColor: element.statusBarColor,
                  hideAppBar: element.hideAppBar,
                ));
          },
          child: Column(
            children: [
              Image.network(
                element.icon!,
                width: 18,
                height: 18,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 3),
                child: Text(
                  element.title!,
                  style: const TextStyle(fontSize: 12),
                ),
              )
            ],
          ),
        ));
  }
}
